﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using Models;
using System.Data;


public partial class TagList : System.Web.UI.Page//AdminBase
{

    private DataTable dc = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        grdbind();
    }

    public void grdbind()
    {
        using (Entities bll = new Entities())
        {
            this.rptList.DataSource = bll.U_TX.OrderByDescending(p => p.TXTime).ToList();
            this.rptList.DataBind();
        }

        using (Entities bll = new Entities())
        {

            var Bpl = from j in bll.U_TX.OrderByDescending(p => p.TXTime) select j;

            if (txtUtel.Text.Trim() != "")
            {
                var user = bll.User.SingleOrDefault(p => p.UTrueName == txtUtel.Text.Trim() || p.UNickName== txtUtel.Text.Trim() || p.UTel== txtUtel.Text.Trim());
                if (user != null)
                {
                    Bpl = Bpl.Where(p => p.UGuid == user.UGuid);
                }
            }

            if (ddlState.SelectedValue != "请选择")
            {
                int type = int.Parse(ddlState.SelectedValue);
                Bpl = Bpl.Where(p => p.TXstat == type);
            }

            //时间查询
            //业务逻辑：如果选择了两个时间，则按时间段查询，否则按单个时间查询
            if (!string.IsNullOrEmpty(txtStartTime.Text.Trim()) && !string.IsNullOrEmpty(txtEndtime.Text.Trim()))//按时间段查询
            {
                DateTime starttime = Convert.ToDateTime(txtStartTime.Text.Trim());
                DateTime endtime = Convert.ToDateTime(txtEndtime.Text.Trim());
                Bpl = Bpl.Where(p => p.TXTime > starttime && p.TXTime <= endtime);
            }
            else //根据单个时间查询
            {
                if (!string.IsNullOrEmpty(txtStartTime.Text.Trim()))
                {
                    DateTime startime = Convert.ToDateTime(txtStartTime.Text.Trim());
                    Bpl = Bpl.Where(p => p.TXTime == startime);
                }
                else if ((!string.IsNullOrEmpty(txtEndtime.Text.Trim())))
                {
                    DateTime endtime = Convert.ToDateTime(txtEndtime.Text.Trim());
                    Bpl = Bpl.Where(p => p.TXTime == endtime);
                }
            }

            AspNetPager1.RecordCount = Bpl.Count();
            this.rptList.DataSource = Bpl.Skip((AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize).Take(AspNetPager1.PageSize).ToList();  //这里好像一定要Tolist();不然会有点错误；;
            this.rptList.DataBind();
        }
    }

    protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {   //当页面页数改变时，执行这个方法；
        AspNetPager1.CurrentPageIndex = e.NewPageIndex;
        grdbind();
    }

    protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int id = Convert.ToInt32(((Label)e.Item.FindControl("lb_id")).Text);
        Entities bll = new Entities();
        U_TX model = bll.U_TX.First(p => p.TXID == id);
        switch (e.CommandName.ToLower())
        {
            case "del":
                if (model.TXstat == 0)
                {
                    model.TXstat = 1;
                }
                else
                {
                    model.TXstat = 1;
                }
                break;
        }
        bll.SaveChanges();
        grdbind();
    }

    public string getState(int Sta)
    {
        string result = "";
        if ( Sta==0)
        {
            result = "未转账";
        }
        else
        {
            result = "已转账";
        }
        return result;
    }

    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        grdbind();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        grdbind();
    }
}

